<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;

use Hyperf\HttpServer\Contract\RequestInterface;

use Hyperf\DbConnection\Db;
use function Hyperf\Support\env;
use PDO;


#[Controller(prefix: '/employee')]
class EmployeeController extends AbstractController
{
    #[GetMapping(path: '')]
    public function index()
    {
        try {
            // $config = [
            //     'host' => env('DB_HOST', 'mysql'),
            //     'port' => (int)env('DB_PORT', 3306),
            //     'dbname' => env('DB_DATABASE', 'mydatabase'),
            //     'username' => env('DB_USERNAME', 'root'),
            //     'password' => env('DB_PASSWORD', '123456'),
            //     'charset' => env('DB_CHARSET', 'utf8mb4'),
            // ];
            // $pdo = new PDO(
            //     'mysql:host=mysql;port=3306;dbname=mydatabase', // 显式指定端口
            //     'root', 
            //     '123456',
            //     [
            //         PDO::ATTR_EMULATE_PREPARES => false,
            //         PDO::ATTR_STRINGIFY_FETCHES => false,
            //     ]
            // );
            //  // 如果连接成功，执行一个简单的查询来验证连接
            //  $list = $pdo->query("SELECT id, des, des1, des3, tabnm, isdel, updated_at, created_at FROM testtab_2020 LIMIT 100");
            //  $results = $list->fetchAll();

            $count = Db::select(' SELECT count(id) as allcount FROM testtab_2020 ')[0];
            $results = Db::select('SELECT id,des,des1,des3,tabnm,isdel,updated_at,created_at FROM testtab_2020 WHERE 1 LIMIT  100');
            // 查询所有 
            return [
                'code' => 1,
                'count' => $count,
                'message' => "查询成功",
                'data' => $results,

            ];
        } catch (\PDOException $e) {
            return ['status' => 'error', 'message' => $e->getMessage()];
        }
    }

    #[GetMapping(path: 'get')]
    public function get()
    {
        // 查询
        $data = $this->request->all();
        $count = Db::select(' SELECT count(id) as allcount FROM testtab_2020  ')[0];
        $results = Db::select('SELECT id,des,des1,des3,tabnm,isdel,updated_at,created_at FROM testtab_2020 WHERE 1 LIMIT  100');
        return [
            'code' => 1,
            'count' => $count->allcount,
            'message' => "查询成功",
            'data' => $results,

        ];
    }

    // 支持多种HTTP方法
    #[RequestMapping(path: 'save', methods: ['get', 'POST'])]
    public function save()
    {
        // 保存
        $data = $this->request->all();
        if (empty($data)) {
            return ['code' => 0, 'message' => "数据为空", 'param' => $data];
        }
        $des = '';
        if (empty($data['id'])) {
            $des = '新增';
            $ids = Db::select(' SELECT max(id) as id FROM testtab_2020  ')[0];
            Db::table('testtab_2020')->insert([
                ['id'=>$ids->id++,'des' => $data['des'], 'des1' => $data['des1'], 'des3' => $data['des3'], 'created_at' => date('Y-m-d H:i:s')]
            ]);
        } else {
            $des = '修改';
            Db::table('testtab_2020')->where('id', $data['id'])
                ->update(['des' => $data['des'], 'des1' => $data['des1'], 'des3' => $data['des3'], 'updated_at' => date('Y-m-d H:i:s')]);
        }
        return ['code' => 1, 'message' => $des . "成功", 'param' => $data];
    }

    #[GetMapping(path: 'delete')]
    public function delete()
    {
        //删除
        $data = $this->request->all();
        if (empty($data['id'])) {
            return ['code' => 0, 'message' => "需要删除的记录为空"];
        }
        Db::table('testtab_2020')->where('id', '=', $data['id'])->delete();
        return ['code' => 1, 'message' => "删除成功", 'data' => $data];
    }
 

    
}